<template>
  <view class="content">
    <view class="record_wrapper">
      <view
        class="record"
        v-for="(item, index) in productList"
        @click="handleCheck(index)"
      >
        <image class="record_image" :src="item.product.cover"></image>

        <view class="record_content">
          <view class="record_content_title">{{ item.product.name }}</view>

          <view class="record_content_CollectionCode">
            <image
              class="record_content_CollectionIcon"
              src="../../static/collect_icon.png"
            ></image>

            <text class="record_content_no">{{ item.no }}</text>
          </view>

          <view class="record_content_second">
            <image
              class="record_content_avatar"
              :src="item.product.creator_avatar"
            ></image>

            <view class="record_content_username">{{
              item.product.creator_name
            }}</view>

            <view class="integral"
              >{{ item.product.zm_config.exchange_rate }} 积分</view
            >
          </view>

          <image
            v-if="item.check"
            class="allcheck_icon"
            src="../../static/Selected.png"
          ></image>
          <image
            v-else
            class="allcheck_icon"
            src="../../static/unchecked.png"
          ></image>
        </view>
      </view>

      <empty :show="productList.length == 0 && is_done"></empty>
    </view>

    <view class="fixed">
      <view class="left">
        <view class="allcheck" @click="handleAllCheck">
          <image
            v-if="is_allCheck"
            class="allcheck_icon"
            src="../../static/Selected.png"
          ></image>
          <image
            v-else
            class="allcheck_icon"
            src="../../static/unchecked.png"
          ></image>

          <text class="text">全选</text>
        </view>

        <view class="notice"
          >（已选{{ check_num }}个 =
          <text class="highlight">{{ check_point }} 积分</text>）</view
        >
      </view>
      <view class="right" @click="handleExchange">立即兑换</view>
    </view>
  </view>
</template>

<script>
import api from "../../api/api";
import tip from "@/utils/tip.js";
export default {
  data() {
    return {
      is_allCheck: false,
      productList: [],
      is_done: false,
      check_num: 0,
      check_point: 0,
      checkArr: [],
      detail: {},
    };
  },
  computed: {
    // check_num_computed() {
    //   return this.productList.filter((x) => x.check).length;
    // },
  },
  onLoad(options) {},
  onShow() {
    this.getList(true);
  },
  // 页面的触底事件
  onReachBottom() {
    this.page++;
    this.$u.throttle(this.getList(), 1000, true);
  },
  methods: {
    handleCheck(index) {
      this.productList[index].check = !this.productList[index].check;

      this.calculate();
      this.$forceUpdate();
    },

    handleAllCheck() {
      this.productList.forEach((x) => {
        this.is_allCheck ? (x.check = false) : (x.check = true);
      });
      this.is_allCheck = !this.is_allCheck;

      this.calculate();
      this.$forceUpdate();
    },

    calculate() {
      this.checkArr = this.productList.filter((x) => x.check);
      this.check_num = this.checkArr.length;
      let check_point = this.checkArr.reduce((pre, now) => {
        return pre + Number(now.product.zm_config.exchange_rate);
      }, 0);

      this.check_point = check_point.toFixed(2);
    },

    async getList(refresh) {
      if (refresh) {
        this.page = 1;
      }
      let res = await api.zm_wallet({
        method: "get",
        query: {
          page: this.page,
          limit: this.limit,
        },
      });
      if (res.code === 0) {
        this.is_done = true;
        this.detail = res.data;
        if (refresh) {
          this.productList = res.data.data;
        } else {
          this.productList = this.productList.concat(res.data.data);
        }

        this.productList.forEach((x) => {
          x.check = false;
        });
      } else {
        tip.defaults(res.msg);
      }
    },

    async handleExchange() {
      let user_product_ids = this.checkArr.map((x) => x.id);
      if (user_product_ids.length == 0) {
        tip.defaults("至少需要选择一个");
        return;
      }
      let res = await api.exchange({
        method: "post",
        query: {
          user_product_ids: user_product_ids,
        },
      });

      if (res.code == 0) {
        tip.defaults("兑换成功");
        this.getList(true);
      } else {
        tip.defaults(res.msg);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.content {
  width: 100%;
  height: 100%;
  background-color: $bg-color1;
  //   min-height: 100vh;
  min-height: calc(100vh - 44px);
}
.record_wrapper {
  padding: 30rpx;
  width: 750rpx;
}
.record {
  display: flex;
  align-items: center;
  width: 690rpx;
  height: 242rpx;
  //   border-radius: 24rpx;

  background-image: url(../../static/transaction_record_bg.png);
  background-repeat: no-repeat;
  background-size: 100% 242rpx;
  &_image {
    width: 242rpx;
    height: 242rpx;
    // border-radius: 24rpx;
  }
  &_content {
    position: relative;
    flex: 1;
    padding: 30rpx;
    &_title {
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: $text-color1;
      line-height: 44rpx;
      overflow: hidden;
      -webkit-line-clamp: 1;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
    }
    &_second {
      width: 388rpx;
      position: relative;
      margin-top: 20rpx;
      display: flex;
      align-items: center;
      // .integral {
      //   position: absolute;
      //   right: 0;
      //   height: 40rpx;
      //   font-size: 28rpx;
      //   font-weight: 500;
      //   color: #1fe6e6;
      //   line-height: 40rpx;
      // }
      .integral {
        text-align: right;
        height: 40rpx;
        font-size: 28rpx;
        font-weight: 500;
        color: #1fe6e6;
        line-height: 40rpx;
      }
    }
    &_avatar {
      width: 36rpx;
      height: 36rpx;
      border-radius: 50%;
    }
    &_username {
      flex: 1;
      margin-left: 8rpx;
      height: 36rpx;
      font-size: 26rpx;
      font-weight: 500;
      color: $text-color1;
      line-height: 36rpx;

      max-width: 300rpx;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &_third {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 40rpx;
    }
    &_price {
      height: 42rpx;
      font-size: 36rpx;
      font-family: SFPro-Bold, SFPro;
      font-weight: bold;
      color: $text-color1;
      line-height: 42rpx;
    }
    &_status {
      height: 34rpx;
      font-size: 24rpx;
      font-weight: 400;
      color: #39a2ff;
      line-height: 34rpx;
    }
    &_CollectionCode {
      padding: 0 20rpx 0 30rpx;
      display: inline-block;

      margin-top: 40rpx;
      position: relative;
      left: 16rpx;
      // width: 330rpx;
      height: 34rpx;
      color: #ffffff;
      background: $gradient1;
      border-radius: 6rpx;

      font-size: 24rpx;
      font-weight: 400;
      line-height: 34rpx;
      text-align: center;

      height: 32rpx;
    }
    &_CollectionIcon {
      position: absolute;
      top: -10rpx;
      left: -26rpx;
      width: 52rpx;
      height: 52rpx;
    }
    &_no {
      display: inline-block;
      max-width: 270rpx;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
  .allcheck_icon {
    position: absolute;
    top: 102rpx;
    right: 30rpx;
    width: 40rpx;
    height: 40rpx;
  }
}
.record + .record {
  margin-top: 30rpx;
}
.fixed {
  padding: 0 30rpx;
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;

  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 750rpx;
  height: 112rpx;
  background: #292b2b;
  .left {
    display: flex;
    align-items: center;
    .allcheck {
      display: flex;
      align-items: center;
      .allcheck_icon {
        width: 28rpx;
        height: 28rpx;
      }
      .text {
        margin-left: 10rpx;
        width: 56rpx;
        height: 40rpx;
        font-size: 28rpx;
        font-weight: 400;
        color: #ffffff;
        line-height: 40rpx;
      }
    }
    .notice {
      margin-left: 30rpx;
      //   width: 286rpx;
      height: 40rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 40rpx;
    }
    .highlight {
      color: #1fe7e7;
    }
  }
  .right {
    width: 192rpx;
    height: 68rpx;
    background: linear-gradient(90deg, #1fe6e6 0%, #3f9ed2 48%, #df7fd6 100%);
    border-radius: 34rpx;
    text-align: center;
    font-size: 28rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 68rpx;
  }
}
</style>